﻿using Libraries.Entities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Libraries.Repositories
{
    public class ArticleRepository
    {
        /// <summary>
        /// To the entity.
        /// </summary>
        /// <param name="records">The records.</param>
        /// <returns></returns>
        public static Article ToEntity(IDataRecord records)
        {
            return new Article()
            {
                Id = (int)records["Id"],
                Title = records["Title"].ToStringOrDefault(),  
                Contents = records["Contents"].ToStringOrDefault(),
                DateCreated = (DateTime)records["DateCreated"],
                StartDate = (DateTime)records["StartDate"],
                Expried = (DateTime)records["Expried"],
                FilePath = records["FilePath"].ToStringOrDefault(),
                Location = records["Location"].ToStringOrDefault(),
                IsPublic = (bool)records["IsPublic"],
                IsDeleted = (bool)records["IsDeleted"]
            };
        }

        /// <summary>
        /// Gets the article paging.
        /// </summary>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="TotalRecords">The total records.</param>
        /// <returns></returns>
        public Collection<Article> GetArticlePaging(int pageSize, int pageNumber, out int TotalRecords)
        {
            Collection<Article> list = new Collection<Article>();
            Collection<SqlParameter> param = new Collection<SqlParameter> {
             new SqlParameter("@PageSize", pageSize),new SqlParameter("@CurrentPage", pageNumber) };
            TotalRecords = 0;

            using (IDataReader result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.FetchAllArticle, param, ConnectionConfiguration.ConnectionString))
            {
                if (result != null)
                {
                    while (result.Read())
                    {
                        list.Add(ToEntity(result));
                        //TotalRecords = ((int)result["RowNumber1"] + (int)result["RowNumber2"]) - 1;
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// Gets the article by id.
        /// </summary>
        /// <param name="articleId">The article id.</param>
        /// <returns></returns>
        public Article GetArticleById(int articleId)
        {
            using (IDataReader result = RepositorySql.SqlHelper.ExecuteReader(ProcedureConfiguration.FetchArticleById, new Collection<SqlParameter> { new SqlParameter("@ArticleId", articleId) }, ConnectionConfiguration.ConnectionString))
            {
                if (result != null)
                {
                    while (result.Read())
                    {
                        if ((int)result["Id"] == articleId)
                        {
                            return ToEntity(result);
                        }
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Updates the article.
        /// </summary>
        /// <param name="article">The article.</param>
        /// <returns></returns>
        public bool UpdateArticle(Article article)
        {
            Collection<SqlParameter> param = new Collection<SqlParameter>();
            param.Add(new SqlParameter("@Id", article.Id));
            param.Add(new SqlParameter("@Title", article.Title));
            param.Add(new SqlParameter("@Contents", article.Contents));
            param.Add(new SqlParameter("@DateCreated", article.DateCreated));
            param.Add(new SqlParameter("@StartDate", article.StartDate));
            param.Add(new SqlParameter("@Expried", article.Expried));
            param.Add(new SqlParameter("@FilePath", article.FilePath));
            param.Add(new SqlParameter("@Location", article.Location));
            param.Add(new SqlParameter("@IsPublic", article.IsPublic));
            param.Add(new SqlParameter("@IsDeleted", article.IsDeleted));

            if (article.Id == 0)
            {
                int result = RepositorySql.SqlHelper.ExecuteNonQuery(ProcedureConfiguration.AddArticle, param, ConnectionConfiguration.ConnectionString);
                return result > 0;
            }
            else
            {
                var result = RepositorySql.SqlHelper.ExecuteNonQuery(ProcedureConfiguration.UpdateArticle, param, ConnectionConfiguration.ConnectionString);
                return result > 0;
            }
        }
    }
}
